import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import NProgress from 'nprogress';
import layout from '../views/layout/index.vue';
import store from '../store/index';

NProgress.configure({
  easing: 'ease', // 动画方式
  speed: 300, // 递增进度条的速度
  showSpinner: true, // 是否显示加载 icon
  trickleSpeed: 300, // 自动递增间隔
  minimum: 0.5 // 初始化时的最小百分比
});

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '',
        name: 'home',
        component: () => import('../views/home/index.vue'),
        meta: {
          title: '首页'
        }
      },
      {
        path: '/yield',
        name: 'yield',
        component: () => import('../views/yield/index.vue'),
        meta: {
          title: '生产'
        }
      },
      {
        path: '/repertory',
        name: 'repertory',
        component: () => import('../views/repertory/index.vue'),
        meta: {
          title: '库存'
        }
      },
      {
        path: '/equipment',
        name: 'equipment',
        component: () => import('../views/equipment/index.vue'),
        meta: {
          title: '设备'
        }
      },
      {
        path: '/market',
        name: 'market',
        component: () => import('../views/market/index.vue'),
        meta: {
          title: '销售'
        }
      },
      {
        path: '/publicity',
        name: 'publicity',
        component: () => import('../views/publicity/index.vue'),
        meta: {
          title: '宣传'
        }
      }
    ]
  },
  {
    path: '/doc',
    name: 'doc',
    component: () => import('../views/doc/index.vue'),
    meta: {
      title: '使用文档'
    }
  },
  {
    path: '/:catchAll(.*)',
    name: 'error',
    component: () => import('../views/error/index.vue'),
    meta: {
      title: '错误页面'
    }
  }
];

const router = createRouter({
  history: createWebHashHistory(),
  routes
});

router.beforeEach((to: any, _: any, next: any) => {
  NProgress.start();
  document.title = `${to.meta.title || ''} | 中南烟花智慧工厂`;
  store.commit('setActivePage', to.name);
  next();
});

router.afterEach(() => {
  NProgress.done(); // 关闭进度条
});

export default router;
